package com.alienware.carlauncherlite;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alienware.carlauncherlite.Dock.DockAdapter;
import com.alienware.carlauncherlite.Dock.DockDialog;
import com.alienware.carlauncherlite.Dock.DockItemInfo;
import com.alienware.carlauncherlite.Dock.DockItemManager;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class LauncherActivity extends AppCompatActivity {
    private static TextView dockTexViewTime = null;
    private static TextView dockTexViewDate = null;
    private final Handler handler = new Handler();

    private static List<DockItemInfo> dockItemInfoList = new ArrayList<DockItemInfo>();
    private static HashSet<String> dockAppHashSet = new HashSet<String>();
    private static GridView dockGridView = null;
    private static DockAdapter dockAdapter = null;
    private static View dockGridViewLinearLayout = null;

    private static List<DockItemInfo> allItemInfoList = new ArrayList<DockItemInfo>();
    private static DockDialog dockDialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_launcher);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        //==========Dock==========
        initDock();

        //==========Dialog==========
        initDialog();

        //==========Handler==========
        handler.postDelayed(runnable, 1000);
    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            dockTexViewTime.setText(TimeUtils.getNowTime());
            dockTexViewDate.setText(TimeUtils.getNowDate());
            handler.postDelayed(this, 1000);
        }
    };

    private void initDock() {
        dockTexViewTime = (TextView) findViewById(R.id.dockTextViewTime);
        dockTexViewTime.setText(TimeUtils.getNowTime());
        dockTexViewDate = (TextView) findViewById(R.id.dockTextViewDate);
        dockTexViewDate.setText(TimeUtils.getNowDate());

        dockItemInfoList = DockItemManager.GetDockAppList(getApplicationContext());
        dockAppHashSet = DockItemManager.GetDockAppHashSet();
        dockGridView = (GridView) findViewById(R.id.dockGridViewApps);
        dockGridViewLinearLayout = (View) findViewById(R.id.dockGridViewLinearLayout);

        setDockStyle();

        dockAdapter = new DockAdapter(dockItemInfoList, getApplicationContext());
        dockGridView.setAdapter(dockAdapter);

        //监听
        dockGridView.setOnItemClickListener((adapterView, view, i, l) -> {
            DockItemInfo dockItemInfo = dockItemInfoList.get(i);
            String packageName = dockItemInfo.getPackageName();
            if (packageName.equals(DockItemManager.allAppItemName)) {
                dockDialog.show();
                dockDialog.getAllAppsGridView().setOnItemLongClickListener((adapterView2, view2, i2, l2) -> {
                    dockDialog.dismiss();
                    addDockItem(i2);
                    return true;
                });
                dockTexViewTime.setVisibility(View.INVISIBLE);
                dockTexViewDate.setVisibility(View.INVISIBLE);
                dockGridViewLinearLayout.setVisibility(View.INVISIBLE);
            } else {
                Intent intent = getPackageManager().getLaunchIntentForPackage(dockItemInfoList.get(i).getPackageName());
                if (intent != null) {
                    startActivity(intent);
                }
            }
        });
        dockGridView.setOnItemLongClickListener((adapterView, view, i, l) -> {
            DockItemInfo dockItemInfo = dockItemInfoList.get(i);
            String packageName = dockItemInfo.getPackageName();
            if (packageName.equals(DockItemManager.allAppItemName)) {
                Toast.makeText(getApplicationContext(), R.string.app_hide_failed, Toast.LENGTH_LONG).show();
            } else {
                removeDockItem(i);
            }
            return true;
        });
    }

    private void setDockStyle() {
        //设置单行显示
        int length = 100;
        int size = dockItemInfoList.size();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        float density = displayMetrics.density;
        int gridviewWidth = (int) (size * (length + 4) * density);
        int itemWidth = (int) (length * density);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridviewWidth, LinearLayout.LayoutParams.MATCH_PARENT);
        dockGridView.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
        dockGridView.setColumnWidth(itemWidth); // 设置列表项宽
        dockGridView.setStretchMode(GridView.STRETCH_SPACING_UNIFORM);
        dockGridView.setNumColumns(size); // 设置列数量=列表集合数
    }

    private void addDockItem(int i) {
        if (DockItemManager.addDockItem(LauncherActivity.this, allItemInfoList.get(i).getPackageName())) {
            dockItemInfoList.add(allItemInfoList.get(i));
            dockAdapter.notifyDataSetChanged();
            setDockStyle();
        }
    }

    private void removeDockItem(int i) {
        if (DockItemManager.removeDockItem(LauncherActivity.this, dockItemInfoList.get(i).getPackageName())) {
            dockItemInfoList.remove(i);
            dockAdapter.notifyDataSetChanged();
            setDockStyle();
        } else {
            Toast.makeText(getApplicationContext(), R.string.app_hide_failed, Toast.LENGTH_LONG).show();
        }
    }

    private void initDialog() {
        //获取数据
        allItemInfoList = DockItemManager.GetAllAppList(LauncherActivity.this);
        dockDialog = new DockDialog(LauncherActivity.this, allItemInfoList);
        //监听Dialog隐藏
        dockDialog.setOnDismissListener(dialogInterface -> {
            dockTexViewTime.setVisibility(View.VISIBLE);
            dockTexViewDate.setVisibility(View.VISIBLE);
            dockGridViewLinearLayout.setVisibility(View.VISIBLE);
        });
    }
}